package gv.utils 
{

	/**
	 * @author rick
	 */
	public class Utils 
	{
		public static function in_array(needle : *, haystack : Array) : Boolean
		{
			for each(var elem:* in haystack)
			{
				if(elem == needle)
				{
					return true;
				}
			}
			return false;
		}

		public static function selectRandomInArray(inputArray : Array, count : int, exclude : int = -1) : Array
		{
			var array : Array = new Array();
			if(inputArray.length > 0)
			{
				if(count > inputArray.length)
				{
					count = inputArray.length;
				}
				for(var c : int = 0;c < count;c++)
				{
					var notfound : Boolean = true;
					while(notfound)
					{
						var i : int = Math.floor(Math.random() * inputArray.length);
						if(Utils.in_array(inputArray[i], array) == false && i != exclude)
						{
							array.push(inputArray[i]);
							notfound = false;
						}
					}
				}
			}
			return array;
		}

		public static function randomizeArray(srcArray : Array) : Array 
		{
			var returnArray : Array = new Array();
			var orgArray : Array = srcArray.slice();
			while (orgArray.length > 0) 
			{
				var r : uint = Math.floor(Math.random() * orgArray.length);
				returnArray.push(orgArray.splice(r, 1)[0]);
			}
			return returnArray;
		}
	}
}
